# coding=utf-8
import re
import urllib2

import os


class Spider:
    def __init__(self):
        self.site_url = 'http://mm.taobao.com/json/request_top_list.htm'

    def get_page(self, page_idx):
        url = self.site_url + '?page=' + str(page_idx)
        request = urllib2.Request(url)
        response = urllib2.urlopen(request)
        return response.read().decode('GBK')

    def get_content(self, page_idx):
        page = self.get_page(page_idx)
        pattern = re.compile(
            '<a href="//(mm.taobao.com/\d{7,12}.htm)".*?<img src="//(.*?)".*?target="_blank">(.*?)</a>.*?<strong>(.*?)</strong>.*?<span>(.*?)</span>',
            re.S)
        items = re.findall(pattern, page)
        return items

    def make_dir(self, path):
        path = path.strip()
        if not os.path.exists(path):
            os.makedirs(path)
            return True
        else:
            return False

    def save_img(self, image_url, file_name):
        if os.path.exists(file_name):
            return
        else:
            try:
                request = urllib2.Request(image_url)
                img = urllib2.urlopen(request).read()
                img_file = open(file_name, 'wb')
                img_file.write(img)
                img_file.close
            except urllib2.URLError:
                print 'url error : ' + image_url

    def get_detail_img_by_url(self, url):
        request = urllib2.Request(url)
        page = urllib2.urlopen(request).read()
        pattern = re.compile('<img style="float: none.*? src="//(img.alicdn.com.*?)"', re.S)
        content = re.findall(pattern, page)
        rtn = []
        for i in content:
            rtn.append('http://' + i)
        return rtn


spider = Spider()
for i in range(13):
    items = spider.get_content(i)
    for item in items:
        detail_img = spider.get_detail_img_by_url('http://' + item[0])
        spider.make_dir('img\\' + item[2])
        cnt = 0
        print 'total photos :' + str(len(detail_img))
        for u in detail_img:
            cnt = cnt + 1
            print 'saving ' + item[2] + ' ' + str(cnt)
            spider.save_img(u, 'img\\' + item[2] + "\\" + str(cnt) + '.jpg')
